﻿@{
    ViewBag.Title = "AddContent";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var _Category = request('Category');
    var organizeId = request('organizeId');
    var _rowid = request('rowid');
    var row_id = request('row_id');
    $(function () {
        GetGrid();
    });

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var queryJson = new Object();
        queryJson["organizeId"] = organizeId;
        queryJson["Category"] = _Category;
        var $gridTable = $('#gridTable');
        $gridTable.jqGrid({
            url: "/DailyEventManage/OpenShop/GetContent",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 141.5,
            autowidth: true,
            multiselect: true,
            colModel: [
                { label: "主键", name: "Id", index: "Id", hidden: true },
                { label: "编号", name: "Code", index: "Code", width: 70, align: "center" },
                { label: "名称", name: "Name", index: "Name", width: 100, align: "center" },
                {
                    label: "类型", name: "Type", index: "Type", width: 50, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == 1 ? "选择型" : "录入型";
                    }
                },
                {
                    label: "内容详情", name: "TypeDecription", index: "TypeDecription", width: 300, align: "left",
                    formatter: function (cellvalue, options, rowObject) {
                        var obj = JSON.parse(cellvalue);
                        var _Html = "";
                        if (rowObject.Type == 1) {
                            $.each(obj, function (index, item) {
                                _Html += "<div style=\"float: left; margin-left: 20px;\">" + item.Text + "</div>";
                            });
                            return _Html;
                        }
                        else {
                            if (obj[0].ValueType == "文本型") {
                                _Html = "默认值：";
                                _Html += obj[0].DefaultValue == "" ? "无" : obj[0].DefaultValue;
                            }
                            else {
                                _Html = "最小值：";
                                _Html += obj[0].MinValue == "" ? "无" : obj[0].MinValue;
                                _Html += ",最大值：";
                                _Html += obj[0].MaxValue == "" ? "无" : obj[0].MaxValue;
                            }
                        }
                        return _Html;
                    }
                },
                {
                    label: "是否必填", name: "IsNeed", index: "IsNeed", width: 80, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        return cellvalue == 1 ? "<i value=" + cellvalue + " class=\"fa fa-toggle-on\"></i>" : "<i value=" + cellvalue + " class=\"fa fa-toggle-off\"></i>";
                    }
                },
                { label: "说明", name: "Memo", index: "Memo", width: 150, align: "left" }
            ],
            rownumbers: true,
            rowNum: 1000,
            shrinkToFit: false,
            gridview: true,
            onSelectRow: function () {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = new Object();
            queryJson["organizeId"] = organizeId;
            queryJson["Category"] = _Category;
            queryJson["keyword"] = $("#txt_Keyword").val();
            $gridTable.jqGrid('setGridParam', {
                url: "../../DailyEventManage/OpenShop/GetContent",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });
        //查询回车
        $('#txt_Keyword').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                $('#btn_Search').trigger("click");
            }
        });
    }


    function TopMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            if ($tr.index() != 1)
                $tr.prevAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function SortUp() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            if ($tr.index() != 1)
                $tr.prev().before($tr);
            $tr.getGridParam('selrow');
        }
    }


    function SortDown() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            $tr.next().after($tr);
            $tr.getGridParam('selrow');
        }
    }

    function DownMost() {
        //var $tr = $(obj).parent().parent();
        var id = $('#gridTable').jqGrid('getGridParam', 'selrow');
        if (checkedRow(id)) {
            $("#gridTable tr td.sgexpanded").trigger("click");
            var $tr = $('#gridTable tr#' + id);
            $tr.nextAll("tr:last").after($tr);
            $tr.getGridParam('selrow');
        }
    }

    //保存表单
    function AcceptClick() {
        var rowids = $('#gridTable').getGridParam('selarrrow');
        var $gridTable = top.frames["Form1"].$("#gridTable");
        if (!!row_id) {
            var ids = "";
            for (var i = 0; i < rowids.length; i++) {
                ids += $('#gridTable').getRowData(rowids[i]).Id + ",";
            }
            $gridTable.setCell(row_id, 'ContentId', ids);
        }
        else {
            for (var i = 0; i < rowids.length; i++) {
                var data = $("#gridTable").jqGrid("getRowData", rowids[i]);
                data["ObjectTypeId"] = 5;
                data["ObjectTypeName"] = "内容";
                data["ContentId"] = data["Id"];
                data["OtherCode"] = data["TypeDecription"];
                delete data["Id"];
                $gridTable.addRowData(_rowid, data);
                _rowid++;
            }
            top.frames["Form1"]._rowid = _rowid;
        }
        dialogMsg("操作成功。", 1);
        dialogClose();
    }
</script>

<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-center">
        <div>
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td style="padding-left: 2px;">
                                <input id="txt_Keyword" type="text" class="form-control" placeholder="请输入要名称或者编号" style="width: 200px;" />
                            </td>
                            <td style="padding-left: 5px;">
                                <a id="btn_Search" class="btn btn-primary"><i class="fa fa-search"></i>&nbsp;查询</a>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <div class="gridPanel">
                <div style="width:94%;float:left;">
                    <table id="gridTable"></table>
                </div>
                <div style="width: 6%; float: right;padding-left:3px;">
                    <a class="btn btn-default" onclick="TopMost();"><i class="fa fa-arrow-up"></i>最上</a><br />
                    <a class="btn btn-default" onclick="SortUp();"><i class="fa fa-arrow-up"></i>向上</a><br />
                    <a class="btn btn-default" onclick="SortDown();"><i class="fa fa-arrow-down"></i>向下</a><br />
                    <a class="btn btn-default" onclick="DownMost();"><i class="fa fa-arrow-down"></i>最下</a><br />
                </div>
            </div>
        </div>
    </div>
</div>
